package sismatriculas;

import BE.UsuarioBE;
import BR.Mensaje;
import BR.UsuarioBR;
import DAO.BD;
import UTILES.Cadenas;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class frmUsuariosSistema extends javax.swing.JInternalFrame {
int cod = 0;
    public frmUsuariosSistema() {
        initComponents();
        GenerarCod();
    } 
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlDatos = new javax.swing.JPanel();
        lblNombre = new javax.swing.JLabel();
        lblApellidos = new javax.swing.JLabel();
        lblFecha = new javax.swing.JLabel();
        lblDireccion = new javax.swing.JLabel();
        lblTipoUsuario = new javax.swing.JLabel();
        lblTelfono = new javax.swing.JLabel();
        lblCelular = new javax.swing.JLabel();
        txtdni = new javax.swing.JFormattedTextField();
        txtapellidos = new javax.swing.JTextField();
        txtnombre = new javax.swing.JTextField();
        txttipousuario = new javax.swing.JTextField();
        txtusuario = new javax.swing.JTextField();
        etiImagen = new javax.swing.JLabel();
        btnAbrirFoto = new javax.swing.JButton();
        cbxsexo = new javax.swing.JComboBox();
        txtclave = new javax.swing.JPasswordField();
        btnGuardar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle(".:: usuarios ::.");

        pnlDatos.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Personales"));

        lblNombre.setText("Nombre:");

        lblApellidos.setText("Apellidos:");

        lblFecha.setText("DNI:");

        lblDireccion.setText("Sexo:");

        lblTipoUsuario.setText("Tipo Usuario");

        lblTelfono.setText("Usuario:");

        lblCelular.setText("Clave:");

        etiImagen.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        btnAbrirFoto.setText("...");
        btnAbrirFoto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAbrirFotoActionPerformed(evt);
            }
        });

        cbxsexo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccione", "M", "F" }));

        javax.swing.GroupLayout pnlDatosLayout = new javax.swing.GroupLayout(pnlDatos);
        pnlDatos.setLayout(pnlDatosLayout);
        pnlDatosLayout.setHorizontalGroup(
            pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlDatosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblFecha)
                    .addComponent(lblDireccion)
                    .addComponent(lblApellidos)
                    .addComponent(lblTipoUsuario)
                    .addComponent(lblTelfono)
                    .addComponent(lblCelular)
                    .addComponent(lblNombre))
                .addGap(18, 18, 18)
                .addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtclave)
                    .addComponent(txtusuario)
                    .addComponent(txttipousuario)
                    .addComponent(txtdni)
                    .addComponent(txtapellidos)
                    .addComponent(txtnombre, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
                    .addComponent(cbxsexo, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(55, 55, 55)
                .addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnlDatosLayout.createSequentialGroup()
                        .addComponent(etiImagen, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(24, 24, 24))
                    .addGroup(pnlDatosLayout.createSequentialGroup()
                        .addComponent(btnAbrirFoto)
                        .addGap(63, 63, 63))))
        );
        pnlDatosLayout.setVerticalGroup(
            pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlDatosLayout.createSequentialGroup()
                        .addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblNombre)
                            .addComponent(txtnombre))
                        .addGap(18, 18, 18)
                        .addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblApellidos)
                            .addComponent(txtapellidos))
                        .addGap(18, 18, 18)
                        .addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblFecha)
                            .addComponent(txtdni))
                        .addGap(18, 18, 18)
                        .addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblDireccion)
                            .addComponent(cbxsexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblTipoUsuario)
                            .addComponent(txttipousuario))
                        .addGap(18, 18, 18)
                        .addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblTelfono)
                            .addComponent(txtusuario))
                        .addGap(18, 18, 18)
                        .addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblCelular)
                            .addComponent(txtclave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(pnlDatosLayout.createSequentialGroup()
                        .addComponent(etiImagen, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnAbrirFoto)
                        .addGap(212, 212, 212))))
        );

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/Save1.png"))); // NOI18N
        btnGuardar.setText("Registrar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/Exit1.png"))); // NOI18N
        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnSalir)
                        .addGap(19, 19, 19))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pnlDatos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlDatos, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalir)
                    .addComponent(btnGuardar))
                .addGap(22, 22, 22))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
void GenerarCod(){
    
    String sql="select count(id_usuario)as buscar from Usuarios";
    Connection cn = new BD().obtieneConexion();
    Statement st = null; //sentencia q nos envia a la BD
    ResultSet rs;
        try {
            st = cn.createStatement();
            rs = st.executeQuery(sql);
           if(rs.next())
            {
                cod = rs.getInt("buscar");
                 cod+=1;
            }
        } catch (SQLException ex) {
            Logger.getLogger(frmUsuariosSistema.class.getName()).log(Level.SEVERE, null, ex);
        }
    //txtcodigo.setText(String.valueOf(cod));

}
    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        this.dispose();
        GenerarCod();
    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnAbrirFotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAbrirFotoActionPerformed
    JFileChooser abrir = new JFileChooser();
    int boton = abrir.showOpenDialog(abrir);
    if(boton == JFileChooser.APPROVE_OPTION){
     String camino = abrir.getSelectedFile().toString();
         ImageIcon imagen = new ImageIcon(camino);
     etiImagen.setIcon(imagen);
}
    }//GEN-LAST:event_btnAbrirFotoActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
GenerarCod();
Cadenas c = new Cadenas();  

String id_usuario = String.valueOf(cod);
String nombre = txtnombre.getText();
String apellidos = txtapellidos.getText();
String dni = txtdni.getText();
String usuario = txtusuario.getText();
String clave = txtclave.getText();
String sexo = (String)cbxsexo.getSelectedItem();
Icon foto = etiImagen.getIcon();
String tipo_user = txttipousuario.getText();

if(c.validaNumeros(id_usuario)){
    UsuarioBE objUBE = new UsuarioBE();
    objUBE.setId_usuario(Integer.parseInt(id_usuario));
    
    objUBE.setNombre(nombre);
    objUBE.setApellidos(apellidos);
    objUBE.setDni(dni);
    objUBE.setUsuario(usuario);
    objUBE.setClave(clave);
    objUBE.setSexo(sexo);
    objUBE.setFoto(String.valueOf(foto));
    objUBE.setTipo_user(tipo_user);

    UsuarioBR escuela = new UsuarioBR();
    Mensaje msj;
    msj = escuela.registrar(objUBE);
    JOptionPane.showMessageDialog(null,msj.getmensaje());
    
txtnombre.setText("");
txtapellidos.setText("");
txtdni.setText("");
txtusuario.setText("");
txtclave.setText("");
etiImagen.setIcon(null);
txttipousuario.setText("");
cbxsexo.setSelectedIndex(0);
txtnombre.requestFocus();
}
else{
   JOptionPane.showMessageDialog(null, "Error en Registro","ERROR",JOptionPane.ERROR_MESSAGE);
 }

GenerarCod();

//JOptionPane.showMessageDialog(null, id_usuario+" "+nombre+" "+apellidos+" "+dni+" "+usuario+" "+clave+" "+sexo+" "+foto+" "+tipo_user);

    }//GEN-LAST:event_btnGuardarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAbrirFoto;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox cbxsexo;
    private javax.swing.JLabel etiImagen;
    private javax.swing.JLabel lblApellidos;
    private javax.swing.JLabel lblCelular;
    private javax.swing.JLabel lblDireccion;
    private javax.swing.JLabel lblFecha;
    private javax.swing.JLabel lblNombre;
    private javax.swing.JLabel lblTelfono;
    private javax.swing.JLabel lblTipoUsuario;
    private javax.swing.JPanel pnlDatos;
    private javax.swing.JTextField txtapellidos;
    private javax.swing.JPasswordField txtclave;
    private javax.swing.JFormattedTextField txtdni;
    private javax.swing.JTextField txtnombre;
    private javax.swing.JTextField txttipousuario;
    private javax.swing.JTextField txtusuario;
    // End of variables declaration//GEN-END:variables
}
